\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}

\input{../labStyle}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}

\begin{document}

\license{This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported (\web{http://creativecommons.org/licenses/by-sa/3.0/}). This lab was adapted for OpenIntro by Mine \c{C}etinkaya-Rundel from a lab written by the faculty and TAs of UCLA Statistics.}

\section*{Lab 5: Inference for numerical data}

\subsection*{North Carolina births}

In 2004, the state of North Carolina released a large data set containing information on births recorded in this state. This data set is useful to researchers studying the relation between habits and practices of expectant mothers and the birth of their children. We will work with a random sample of observations from this data set.

\subsection*{Exploratory analysis}
Load the \hlstd{nc} data set into our workspace.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{download.file}\hlstd{(}\hlstr{"http://www.openintro.org/stat/data/nc.RData"}\hlstd{,} \hlkwc{destfile} \hlstd{=} \hlstr{"nc.RData"}\hlstd{)}

\hlkwd{load}\hlstd{(}\hlstr{"nc.RData"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}


We have observations on 13 different variables, some categorical and some numerical. The meaning of each variable is as follows.

\begin{table}[h] \small
\begin{tabular}{r | l}
\hlstd{fage} & father's age in years. \\
\hlstd{mage} & mother's age in years. \\
\hlstd{mature} & maturity status of mother. \\
\hlstd{weeks} & length of pregnancy in weeks. \\
\hlstd{premie} & whether the birth was classified as premature (premie) or full-term. \\
\hlstd{visits} & number of hospital visits during pregnancy. \\
\hlstd{marital} & whether mother is \hlstr{married} or \hlstr{not married} at birth. \\
\hlstd{gained} & weight gained by mother during pregnancy in pounds. \\
\hlstd{weight} & weight of the baby at birth in pounds. \\
\hlstd{lowbirthweight} & whether baby was classified as low birthweight (\hlstr{low}) or not (\hlstr{not low}). \\
\hlstd{gender} & gender of the baby, \hlstr{female} or \hlstr{male}. \\
\hlstd{habit} & status of the mother as a \hlstr{nonsmoker} or a \hlstr{smoker}. \\
\hlstd{whitemom} & whether mom is \hlstr{white} or \hlstr{not white}. \\
\end{tabular}
\end{table}

\begin{exercise}
What are the cases in this data set? How many cases are there in our sample?
\end{exercise}

As a first step in the analysis, we should consider summaries of the data. This can be done using the \hlkwd{summary} command:

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{summary}\hlstd{(nc)}
\end{alltt}
\end{kframe}
\end{knitrout}


As you review the variable summaries, consider which variables are categorical and which are numerical. For numerical variables, are there outliers? If you aren't sure or want to take a closer look at the data, make a graph.

Consider the possible relationship between a mother's smoking habit and the weight of her baby. Plotting the data is a useful first step because it helps us quickly visualize trends, identify strong associations, and develop research questions.

\begin{exercise}
Make a side-by-side boxplot of \hlstd{habit} and \hlstd{weight}. What does the plot highlight about the relationship between these two variables?
\end{exercise}

The box plots show how the medians of the two distributions compare, but we can also compare the means of the distributions using the following function to split the \hlkwd{weight} variable into the \hlkwd{habit} groups, then take the mean of each using the \hlkwd{mean} function.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{by}\hlstd{(nc}\hlopt{$}\hlstd{weight, nc}\hlopt{$}\hlstd{habit, mean)}
\end{alltt}
\end{kframe}
\end{knitrout}

% mean color of function

There is an observed difference, but is this difference statistically significant? In order to answer this question we will conduct a hypothesis test.

\subsection*{Inference}

\begin{exercise}
Check if the conditions necessary for inference are satisfied. Note that you will need to obtain sample sizes to check the conditions. You can compute the group size using the same \hlkwd{by} command above but replacing \hlstd{mean} with \hlstd{length}.
\end{exercise}

\begin{exercise}
Write the hypotheses for testing if the average weights of babies born to smoking and non-smoking mothers are different.
\end{exercise}

Next, we introduce a new function, \hlkwd{inference}, that we will use for conducting hypothesis tests and constructing confidence intervals. 

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{inference}\hlstd{(}\hlkwc{y} \hlstd{= nc}\hlopt{$}\hlstd{weight,} \hlkwc{x} \hlstd{= nc}\hlopt{$}\hlstd{habit,} \hlkwc{est} \hlstd{=} \hlstr{"mean"}\hlstd{,} \hlkwc{type} \hlstd{=} \hlstr{"ht"}\hlstd{,} \hlkwc{null} \hlstd{=} \hlnum{0}\hlstd{,}
          \hlkwc{alternative} \hlstd{=} \hlstr{"twosided"}\hlstd{,} \hlkwc{method} \hlstd{=} \hlstr{"theoretical"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}


Let's pause for a moment to go through the arguments of this custom function.
\begin{itemize}

\item The first argument is \hlkwc{y}, which is the response variable that we are interested in: \hlstd{nc}\hlkwd{\$}\hlstd{weight}.

\item The second argument is the explanatory variable, \hlkwc{x}, which is the variable that splits the data into two groups, smokers and non-smokers: \hlstd{nc}\hlkwd{\$}\hlstd{habit}.

\item The third argument, \hlkwc{est}, is the parameter we're interested in: \hlstr{"mean"} (other options are \hlstr{"median"}, or \hlstr{"proportion"}.)

\item Next we decide on the \hlkwc{type} of inference we want: a hypothesis test (\hlstr{ht}) or a confidence interval (\hlstr{"ci"}).

\item When performing a hypothesis test, we also need to supply the \hlkwc{null} value, which in this case is \hlnum{0}, since the null hypothesis sets the two population means equal to each other.

\item The \hlkwc{alternative} hypothesis can be \hlstr{"less"}, \hlstr{"greater"}, or \hlstr{"twosided"}.

\item Lastly, the \hlkwc{method} of inference can be \hlstr{"theoretical"} or \hlstr{"simulation"} based.
\end{itemize}

\begin{exercise}
Change the \hlkwc{type} argument to \hlstr{"ci"} to construct and record a confidence interval for the difference between the weights of babies born to smoking and non-smoking mothers.
\end{exercise}

By default the function reports an interval for ($\mu_{nonsmoker} - \mu_{smoker}$). We can easily change this order by using the \hlkwc{order} argument:

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{inference}\hlstd{(}\hlkwc{y} \hlstd{= nc}\hlopt{$}\hlstd{weight,} \hlkwc{x} \hlstd{= nc}\hlopt{$}\hlstd{habit,} \hlkwc{est} \hlstd{=} \hlstr{"mean"}\hlstd{,} \hlkwc{type} \hlstd{=} \hlstr{"ci"}\hlstd{,} \hlkwc{null} \hlstd{=} \hlnum{0}\hlstd{,}
          \hlkwc{alternative} \hlstd{=} \hlstr{"twosided"}\hlstd{,} \hlkwc{method} \hlstd{=} \hlstr{"theoretical"}\hlstd{,}
          \hlkwc{order} \hlstd{=} \hlkwd{c}\hlstd{(}\hlstr{"smoker"}\hlstd{,}\hlstr{"nonsmoker"}\hlstd{))}
\end{alltt}
\end{kframe}
\end{knitrout}


\vspace{2cm}

\subsection*{On your own}

\begin{enumerate}

\item Calculate a 95\% confidence interval for the average length of pregnancies (\hlstd{weeks}) and interpret it in context. Note that since you're doing inference on a single population parameter, there is no explanatory variable, so you can omit the \hlkwc{x} variable from the function.

\item Calculate a new confidence interval for the same parameter at the 90\% confidence level. You can change the confidence level by adding a new argument to the function: \hlkwc{conflevel =}\hlnum{0.90}.

\item Conduct a hypothesis test evaluating whether the average weight gained by younger mothers is different than the average weight gained by mature mothers.

\item Now, a non-inference task: Determine the age cutoff for younger and mature mothers. Use a method of your choice, and explain how your method works.

\item Pick a pair of numerical and categorical variables and come up with a research question evaluating the relationship between these variables. Formulate the question in a way that it can be answered using a hypothesis test and/or a confidence interval. Answer your question using the \hlkwd{inference} function, report the statistical results, and also provide an explanation in plain language.

\item What concepts from the textbook are covered in this lab?  What concepts, if any, are not covered in the textbook?  Have you seen these concepts elsewhere, e.g. lecture, discussion section, previous labs, or homework problems?  Be specific in your answer.

\end{enumerate}

 
\end{document}
